/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.news;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.news.News;
import com.unis.service.attach.AttachLinkService;

/**
 * @author Administrator (email:granzy@sohu.com)
 * @since 2013-02-04
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {

    private IDataSet4iBatis iDataSet4iBatis;
    
    private AttachLinkService attachlinkService;

    /****************************************
     * Insert a instace of News
     ****************************************/
    public void insert(News news) throws Exception {
        news.setNewsid(TemplateUtil.genUUID());
        if (!"".equals(news.getSubjecticonId())) {
            AttachLink attachlink = new AttachLink();
            attachlink.setStatmentId("AttachLink.insert");
            attachlink.setEntityid(news.getNewsid());
            String[] fileids = news.getSubjecticonId().split(",");
            for (int i = 0; i < fileids.length; i++) {
                attachlink.setFileid(fileids[i]);
                attachlinkService.insert(attachlink);
            }
        }
        iDataSet4iBatis.insert(news);
    }

    /****************************************
     * Update a instace of News
     ****************************************/
    public void update(News news) throws Exception {
        if (!"".equals(news.getSubjecticonId())) {
            AttachLink attachlink = new AttachLink();
            attachlink.setStatmentId("AttachLink.delete");
            attachlink.setEntityid(news.getNewsid());
            attachlinkService.delete(attachlink);
            attachlink.setStatmentId("AttachLink.insert");
            String[] fileids = news.getSubjecticonId().split(",");
            for (int i = 0; i < fileids.length; i++) {
                attachlink.setFileid(fileids[i]);
                attachlinkService.insert(attachlink);
            }
        }
        iDataSet4iBatis.update(news);
    }

    /****************************************
     * Delete a instace of News
     ****************************************/
    public void delete(News news) throws Exception {
        iDataSet4iBatis.delete(news);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(News news) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(News news) throws Exception {
        String statementId = news.getStatmentId();
        String mainId = news.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> newsList = new ArrayList<BaseModel>();
        for (String id : ids) {
            News model = new News();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            newsList.add(model);
            AttachLink attachlink = new AttachLink();
            attachlink.setStatmentId("AttachLink.delete");
            attachlink.setEntityid(id);
            attachlinkService.delete(attachlink);
        }
        iDataSet4iBatis.batchDelete(newsList);
    }

    /****************************************
     * Query instaces of News
     ****************************************/
    public List<BaseModel> queryAsList(News news) throws Exception {
        String queryListStatement = "";
        if (news.isPaging()) {
            queryListStatement = news.getStatmentId();
            news.setStatmentId("News.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(news);
            news.setTotalCount(baseModel.getTotalCount());
            news.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(news);
    }

    /****************************************
     * Query a instaces of News
     ****************************************/
    public BaseModel queryAsObject(News news) throws Exception {
        return iDataSet4iBatis.queryAsObject(news);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
    
    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    }
}
